package org.bpms.controller.admin;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.bpms.model.admin.Book;
import org.bpms.model.admin.Booklog;
import org.bpms.model.admin.Bookversion;
import org.bpms.model.admin.Suser;
import org.bpms.plugins.bjui.BjuiRender;
import org.bpms.service.admin.BookService;
import org.bpms.service.admin.BooklogService;
import org.bpms.service.admin.BookversionService;

import com.jfinal.aop.Enhancer;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;

/**
 * 档案流转控制器
 * @author hupei
 *
 */
public class ProgressController extends Controller{
	private BookService bookService = Enhancer.enhance(BookService.class);
	private BooklogService booklogService = Enhancer.enhance(BooklogService.class);
	private BookversionService versionService = Enhancer.enhance(BookversionService.class);
	private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	/**
	 * A节点提交
	 */
	public void pasta(){
		String tabid = getPara("tabid" , "");
		Suser user = getSessionAttr("user");
		String delids = getPara("delids" , "");
		String ids[] = delids.split(",");
		if (null != ids && ids.length > 0){
			for (String id : ids){
				String sql = "update book set state='B',ctime=? where id=?";
				Db.update(sql , df.format(new Date()) , id);
				Booklog log = new Booklog();
				log.set("creater", user.get("realname"));
				log.set("dotype", "提交");
				log.set("bookid", id);
				log.set("createrid", user.get("id"));
				log.save();
			}
		}
		render(BjuiRender.success("提交成功！" , false).tabid(tabid));
	}

	/**
	 * B节点回收
	 */
	public void recoveryb(){
		String tabid = getPara("tabid" , "");
		Suser user = getSessionAttr("user");
		String delids = getPara("delids" , "");
		String ids[] = delids.split(",");
		if (null != ids && ids.length > 0){
			for (String id : ids){
				Booklog logtemp = booklogService.findLast(id);
				String sql = "update book set state='A' , creater=? , ctime=? where id=?";
				Db.update(sql , logtemp.getStr("createrid") , df.format(new Date()) , id);
				Booklog log = new Booklog();
				log.set("creater", user.get("realname"));
				log.set("dotype", "回收");
				log.set("bookid", id);
				log.set("createrid", user.get("id"));
				log.save();
			}
		}
		render(BjuiRender.success("回收成功！" , false).tabid(tabid));		
	}
	
	/**
	 * C节点接收
	 */
	public void pastc(){
		String tabid = getPara("tabid" , "");
		Suser user = getSessionAttr("user");
		String delids = getPara("delids" , "");
		String ids[] = delids.split(",");
		if (null != ids && ids.length > 0){
			for (String id : ids){
				String sql = "update book set state='C' , color=0 , creater=?, ctime=? where id=?";
				Book book = Book.me.findById(id);
				Db.update(sql , user.get("id") , df.format(new Date()) ,id);
				Booklog log = new Booklog();
				log.set("creater", user.get("realname"));
				log.set("dotype", "接收");
				log.set("bookid", id);
				log.set("createrid", user.get("id"));
				log.save();
				Bookversion lastversion = versionService.findLast(book.getStr("bookid"));
				Bookversion version = new Bookversion();
				version.setAttrs(book.getAll());
				version.set("id", null);
				if (null == lastversion){
					version.set("version", 0);
				}else{
					version.set("version", lastversion.getInt("version")+1);
				}
				versionService.save(version);
			}
		}
		render(BjuiRender.success("接收成功！" , false).tabid(tabid));		
	}
	
	/**
	 * C节点回退
	 */
	public void refusec(){
		String tabid = getPara("tabid" , "");
		String reason = getPara("reason" , "");
		Suser user = getSessionAttr("user");
		String delids = getPara("delids" , "");
		String ids[] = delids.split(",");
		if (null != ids && ids.length > 0){
			for (String id : ids){
				Booklog logtemp = booklogService.findLast(id);
				String sql = "update book set state='A' , color=1 , creater=?, ctime=? where id=?";
				Db.update(sql , logtemp.getStr("createrid") , df.format(new Date()) ,id);
				Booklog log = new Booklog();
				log.set("creater", user.get("realname"));
				log.set("dotype", "退回");
				log.set("reason", reason);
				log.set("bookid", id);
				log.set("createrid", user.get("id"));
				log.save();
			}
		}
		render(BjuiRender.success("回退成功！" , true).tabid(tabid));		
	}
	
	/**
	 * c节点确认回退
	 */
	public void isrefusec(){
		Map<String, Object> map = new HashMap<String , Object>();
		String delids = getPara("delids" , "");
		map.put("delids", delids);
		renderJson(map);
	}
	
	/**
	 * c节点回退原因页面
	 */
	public void refusecpage(){
		String tabid = getPara("tabid" , "");
		String delids = getPara("delids" , "");
		setAttr("tabid", tabid);
		setAttr("delids", delids);
		render("/admin/book/refusec.html");
	}
	
	/**
	 * C节点通过
	 */
	public void passc(){
		String tabid = getPara("tabid" , "");
		Suser user = getSessionAttr("user");
		String delids = getPara("delids" , "");
		String ids[] = delids.split(",");
		if (null != ids && ids.length > 0){
			for (String id : ids){
				Booklog logtemp = booklogService.findLast(id);
				String sql = "update book set state='D' , creater=? , ctime=? where id=?";
				Db.update(sql , user.get("id"), df.format(new Date()) , id);
				Booklog log = new Booklog();
				log.set("creater", user.get("realname"));
				log.set("dotype", "通过");
				log.set("bookid", id);
				log.set("createrid", user.get("id"));
				log.save();
			}
		}
		render(BjuiRender.success("检查成功！" , false).tabid(tabid));		
	}
	
	/**
	 * D节点提交
	 */
	public void pastd(){
		String tabid = getPara("tabid" , "");
		Suser user = getSessionAttr("user");
		String delids = getPara("delids" , "");
		String ids[] = delids.split(",");
		if (null != ids && ids.length > 0){
			for (String id : ids){
				String sql = "update book set state='E', ctime=? where id=?";
				Db.update(sql , df.format(new Date()), id);
				Booklog log = new Booklog();
				log.set("creater", user.get("realname"));
				log.set("dotype", "提交");
				log.set("bookid", id);
				log.set("createrid", user.get("id"));
				log.save();
			}
		}
		render(BjuiRender.success("提交成功！" , false).tabid(tabid));
	}
	/**
	 * E节点回收
	 */
	public void recoverye(){
		String tabid = getPara("tabid" , "");
		Suser user = getSessionAttr("user");
		String delids = getPara("delids" , "");
		String ids[] = delids.split(",");
		if (null != ids && ids.length > 0){
			for (String id : ids){
				Booklog logtemp = booklogService.findLast(id);
				String sql = "update book set state='D' , creater=? ,ctime=? where id=?";
				Db.update(sql , logtemp.getStr("createrid") ,df.format(new Date()), id);
				Booklog log = new Booklog();
				log.set("creater", user.get("realname"));
				log.set("dotype", "回收");
				log.set("bookid", id);
				log.set("createrid", user.get("id"));
				log.save();
			}
		}
		render(BjuiRender.success("回收成功！" , false).tabid(tabid));		
	}	
	/**
	 * F节点接收
	 */
	public void pastf(){
		String tabid = getPara("tabid" , "");
		Suser user = getSessionAttr("user");
		String delids = getPara("delids" , "");
		String ids[] = delids.split(",");
		if (null != ids && ids.length > 0){
			for (String id : ids){
				String sql = "update book set state='F' , color=0 , creater=?,ctime=? where id=?";
				Book book = Book.me.findById(id);
				Db.update(sql , user.get("id"),df.format(new Date()) , id);
				Booklog log = new Booklog();
				log.set("creater", user.get("realname"));
				log.set("dotype", "接收");
				log.set("bookid", id);
				log.set("createrid", user.get("id"));
				log.save();
				Bookversion lastversion = versionService.findLast(book.getStr("bookid"));
				Bookversion version = new Bookversion();
				version.setAttrs(book.getAll());
				version.set("id", null);
				if (null == lastversion){
					version.set("version", 0);
				}else{
					version.set("version", lastversion.getInt("version")+1);
				}
				versionService.save(version);
			}
		}
		render(BjuiRender.success("接收成功！" , false).tabid(tabid));		
	}
	
	/**
	 * F节点回退
	 */
	public void refusef(){
		String tabid = getPara("tabid" , "");
		String reason = getPara("reason" , "");
		Suser user = getSessionAttr("user");
		String delids = getPara("delids" , "");
		String ids[] = delids.split(",");
		if (null != ids && ids.length > 0){
			for (String id : ids){
				Booklog logtemp = booklogService.findLast(id);
				String sql = "update book set state='D' , color=1 , creater=?,ctime=? where id=?";
				Db.update(sql , logtemp.getStr("createrid") ,df.format(new Date()) , id);
				Booklog log = new Booklog();
				log.set("creater", user.get("realname"));
				log.set("dotype", "退回");
				log.set("reason", reason);
				log.set("bookid", id);
				log.set("createrid", user.get("id"));
				log.save();
			}
		}
		render(BjuiRender.success("回退成功！" , true).tabid(tabid));		
	}
	
	/**
	 * e节点确认回退
	 */
	public void isrefusee(){
		Map<String, Object> map = new HashMap<String , Object>();
		String delids = getPara("delids" , "");
		map.put("delids", delids);
		renderJson(map);
	}
	
	/**
	 * e节点回退原因页面
	 */
	public void refuseepage(){
		String tabid = getPara("tabid" , "");
		String delids = getPara("delids" , "");
		setAttr("tabid", tabid);
		setAttr("delids", delids);
		render("/admin/book/refusee.html");
	}
	
	/**
	 * F节点完成
	 */
	public void complete(){
		String tabid = getPara("tabid" , "");
		String reason = getPara("reason" , "");
		Suser user = getSessionAttr("user");
		String delids = getPara("delids" , "");
		String ids[] = delids.split(",");
		if (null != ids && ids.length > 0){
			for (String id : ids){
				Booklog logtemp = booklogService.findLast(id);
				String sql = "update book set state='G',ctime=? where id=?";
				Db.update(sql , df.format(new Date()) , id);
				Booklog log = new Booklog();
				log.set("creater", user.get("realname"));
				log.set("dotype", "完成");
				log.set("bookid", id);
				log.set("createrid", user.get("id"));
				log.save();
			}
		}
		render(BjuiRender.success("归档成功！" , false).tabid(tabid));		
	}
	
	/**
	 * 删除
	 */
	public void remove(){
		String tabid = getPara("tabid" , "");
		Suser user = getSessionAttr("user");
		String delids = getPara("delids" , "");
		String ids[] = delids.split(",");
		if (null != ids && ids.length > 0){
			for (String id : ids){
				String sql = "delete from  book where id=?";
				Db.update(sql , id);
			}
		}
		render(BjuiRender.success("删除成功！" , false).tabid(tabid));		
	}	
}
